package controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pojo.Campus;
import pojo.Term;
import service.CampusManager;
import service.TermManager;
import utils.Constents;
import utils.DataParse;
import utils.DataUtils;
import utils.SelectOptionContent;

@Controller
public class OptionScheduleControllerJSP {

	@Autowired
	private CampusManager campusManager;

	@Autowired
	private TermManager termManager;

	@RequestMapping(value = "/jsp/option/schedule", method = RequestMethod.GET)
	public String getOptionSchedule(Map<String, Object> map) {

		List<Term> terms = termManager.getResults(null);
		if (terms.size() == 0) {
			String html = DataUtils.getOptionInfoFromNetWork(Constents.URL_FREEOPTIONKBFB);
			terms = SelectOptionContent.convertToTerm(DataParse.parseHTMLOption(html,"Sel_XNXQ"));
			termManager.addInfoList(terms);
		}
		List<Campus> campuses = campusManager.getResults(null);
		if (campuses.size() == 0) {
			String html = DataUtils.getOptionInfoFromNetWork(Constents.URL_FREEOPTIONKBFB);
			campuses = SelectOptionContent.convertToCampus(DataParse.parseHTMLOption(html, "Sel_XQXX"));

			campusManager.addInfoList(campuses);
		}

		map.put("campuses", campuses);
		map.put("terms", terms);

		return "optionSchedule";
	}

}
